package cs575.wido.ui;

import cs575.client.Task;
import cs575.wido.TaskListFactory;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JTable;

import cs575.wido.action.CompletedTaskAction;
import cs575.wido.action.CurrentTaskAction;
import cs575.wido.action.NewTaskAction;
import cs575.wido.action.PendingTaskAction;
import cs575.wido.action.ViewTaskAction;
import cs575.wido.model.CurrentTaskModel;
import cs575.wido.model.TaskModel;
import javax.swing.JScrollPane;

public class TaskWindow extends JDialog {
	private Map<String, Action> actions;
	private TaskListFactory factory;

	private JPanel buttonPane;
	private JButton btnBack;
	private JLabel lblTask;
	private JComboBox comboBox;
	private JScrollPane scrollPane;
	private JTable taskTable;
	private TaskModel taskModel;
	private JButton btnTaskDetails;
	private JButton btnNewTask;

	/**
	 * Launch the application.
	public static void main(String[] args) {
		try {
			TaskWindow dialog = new TaskWindow();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 */
	
	/**
	 * Create the dialog.
	 */
	public TaskWindow(TaskListFactory factory) {
		this.factory = factory;
		
		setTitle("Task");
		actions = new HashMap<String, Action>();
		buildActions();
		
		getContentPane().add(buildHeaderPane(), BorderLayout.NORTH);
		getContentPane().add(buildButtonPane(), BorderLayout.SOUTH);
		
		taskModel = new CurrentTaskModel(this.factory);
		taskTable = new JTable(taskModel);
		scrollPane = new JScrollPane(taskTable);
		
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		pack();		
	}
	
	private void buildActions() {
		actions.put("current-tasks", new CurrentTaskAction(this));
		actions.put("pending-tasks", new PendingTaskAction(this));
		actions.put("completed-tasks", new CompletedTaskAction(this));
		actions.put("new-task", new NewTaskAction(this));
		//TODO get task here
		actions.put("view-task", new ViewTaskAction(this));
	}
	
	private JComponent buildHeaderPane() {
		JPanel headerPane = new JPanel();
		headerPane.setLayout(new BorderLayout(0, 0));
		
		lblTask = new JLabel("Task");
		lblTask.setFont(new Font("Tahoma", Font.PLAIN, 20));
		headerPane.add(lblTask, BorderLayout.WEST);
		
		comboBox = buildComboBox();
		headerPane.add(comboBox, BorderLayout.EAST);
		
		return headerPane;
	}
	
	private JComponent buildButtonPane() {
		JPanel buttonPane = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPane.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		
		btnNewTask = new JButton("New Task");
		btnNewTask.setAction(actions.get("new-task"));
		buttonPane.add(btnNewTask);
		
		btnTaskDetails = new JButton("Task Details");
		btnTaskDetails.setAction(actions.get("view-task"));
		buttonPane.add(btnTaskDetails);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TaskWindow.this.dispose();
			}
		});
		buttonPane.add(btnBack);
		
		return buttonPane;
	}
	
	private JComboBox buildComboBox() {
		Vector<Action> actionList = new Vector<Action>();
		actionList.add(actions.get("current-tasks"));
		actionList.add(actions.get("pending-tasks"));
		actionList.add(actions.get("completed-tasks"));
		
		JComboBox cb = new JComboBox(actionList);
		cb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				Action action = (Action)cb.getSelectedItem();
				action.actionPerformed(e);		
			}
		});
		return cb;
	}

	public AbstractTableModel getTaskModel() {
		return taskModel;
	}

	public void setTaskModel(TaskModel taskModel) {
		this.taskModel = taskModel;
		this.taskTable.setModel(taskModel);
		this.taskModel.fireTableDataChanged();
	}

	public TaskListFactory getFactory() {
		return factory;
	}

	public void setFactory(TaskListFactory factory) {
		this.factory = factory;
	}
	
	public Task getSelectedTask() {
		int index = this.taskTable.getSelectedRow();
		return this.taskModel.getTask(index);	
	}
        
        public void fireChanges() {
            this.taskModel.fireTableDataChanged();
        }
}
